﻿
namespace Aeon.Emulator.Instructions
{
    internal static class FlagsInstructions
    {
        [Opcode("F8", Name = "clc", AddressSize = 16 | 32, OperandSize = 16 | 32)]
        public static void ClearCarry(VirtualMachine vm)
        {
            vm.Processor.Flags &= ~EFlags.Carry;
        }
        [Opcode("FC", Name = "cld", AddressSize = 16 | 32, OperandSize = 16 | 32)]
        public static void ClearDirection(VirtualMachine vm)
        {
            vm.Processor.Flags &= ~EFlags.Direction;
        }
        [Opcode("FA", Name = "cli", AddressSize = 16 | 32, OperandSize = 16 | 32)]
        public static void ClearInterruptEnable(VirtualMachine vm)
        {
            vm.Processor.Flags &= ~EFlags.InterruptEnable;
        }
        [Opcode("F5", Name = "cmc", AddressSize = 16 | 32, OperandSize = 16 | 32)]
        public static void ComplementCarry(VirtualMachine vm)
        {
            vm.Processor.Flags ^= EFlags.Carry;
        }
        [Opcode("F9", Name = "stc", AddressSize = 16 | 32, OperandSize = 16 | 32)]
        public static void SetCarry(VirtualMachine vm)
        {
            vm.Processor.Flags |= EFlags.Carry;
        }
        [Opcode("FD", Name = "std", AddressSize = 16 | 32, OperandSize = 16 | 32)]
        public static void SetDirection(VirtualMachine vm)
        {
            vm.Processor.Flags |= EFlags.Direction;
        }
        [Opcode("FB", Name = "sti", AddressSize = 16 | 32, OperandSize = 16 | 32)]
        public static void SetInterruptEnable(VirtualMachine vm)
        {
            vm.Processor.Flags |= EFlags.InterruptEnable;
        }
        [Opcode("9F", Name = "lahf")]
        public static void CopyFlagsToAH(VirtualMachine vm)
        {
            vm.Processor.AH = (byte)(vm.Processor.Flags & StoreFlagsMask);
        }
        [Opcode("9E", Name = "sahf", AddressSize = 16 | 32, OperandSize = 16 | 32)]
        public static void CopyAHToFlags(VirtualMachine vm)
        {
            vm.Processor.Flags &= ~StoreFlagsMask;
            vm.Processor.Flags |= ((EFlags)vm.Processor.AH) & StoreFlagsMask;
        }

        private const EFlags StoreFlagsMask = EFlags.Carry | EFlags.Parity | EFlags.Auxiliary | EFlags.Zero | EFlags.Sign;
    }
}
